﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myproject.DTO.entity;
using Myproject.DataAccess.factory;
using Myproject.DataCRM;
using Myproject.Enum;


namespace Myproject.DataAccess.entity
{
    public class RoleProcess : EntityProcess
    {
        public void addRole(Role TRole,Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                        {
                            AddRoleFunction(TRole, dbInstance);
                            TransactionInstance.Complete();
                            TransactionInstance.Dispose();
                            break;
                        }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                        {
                            AddRoleFunction(TRole, dbInstance);
                            break;
                        }
                }
            }
        }

        private void AddRoleFunction(Role TRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.AddToT_ROLE(TRole);
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void updateRole(Role TRole,Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch (TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        UpdateRoleFunction(TRole, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }

                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        UpdateRoleFunction(TRole, dbInstance);
                        break;
                    }
               }
            }
        }

        private void UpdateRoleFunction(Role TRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            T_ROLE roleInstance = dbInstance.T_ROLE.SingleOrDefault(c => c.roleid == TRole.roleid);
            roleInstance.rolename = TRole.rolename;
            roleInstance.roleorder = TRole.roleorder;
            roleInstance.parentid = TRole.parentid;
            roleInstance.status = TRole.status;
            roleInstance.updatetime = DateTime.Now;
            roleInstance.updater = TRole.updater;
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void deleteRole(Role TRole, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        DeleteRoleFunction(TRole, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        DeleteRoleFunction(TRole, dbInstance);
                        break;
                    }
                }
            }
        }

        private void DeleteRoleFunction(Role TRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.DeleteObject(dbInstance.T_ROLE.SingleOrDefault(c => c.roleid == TRole.roleid));
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public List<Role> GeTRoleListByRoleInfo(Role TRole)
        {
            List<Role> result = new List<Role>();
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                var list = from u in dbInstance.T_ROLE
                           where (TRole.roleid == null || TRole.roleid == u.roleid) &&
                                 (TRole.rolename == null || u.rolename.Contains(TRole.rolename)) &&
                                 (TRole.parentid == null || TRole.parentid == u.parentid) &&
                                 (TRole.status == null || TRole.status == u.status) &&
                                 (TRole.StartCreateTime == null || TRole.StartCreateTime <= u.createtime) &&
                                 (TRole.EndCreateTime == null || TRole.EndCreateTime >= u.createtime)
                           select u;
                foreach (var listvalue in list)
                {
                    result.Add(new Role
                    {
                        roleid = listvalue.roleid,
                        rolename = listvalue.rolename,
                        roleorder = listvalue.roleorder,
                        parentid = listvalue.parentid,
                        status = listvalue.status,
                        createtime = listvalue.createtime,
                        updatetime = listvalue.updatetime,
                        author = listvalue.author,
                        updater = listvalue.updater
                    });
                }
            }

            return result;
        }

    }
}
